//
//  UILoadingView.m
//  
//
//  Created by qkong on 15/6/12.
//
//

#import "UILoadingView.h"
#define SPACE_BETWEEN_INDICATOR 10

@interface UILoadingView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIView *indicatorView;

@end

@implementation UILoadingView

- (UIView *)indicatorView{
    if (!_indicatorView) {
        _indicatorView = [[UIView alloc]init];
    }
    return _indicatorView;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        self.delegate = self;
    }
    return self;
}

- (void)awakeFromNib{
    self.delegate = self;
}

- (void)setImages:(NSArray *)images{
    _images = images;
    [self setContentSize:CGSizeMake(images.count*SCREEN_WIDTH, SCREEN_HEIGH)];
    self.scrollEnabled = YES;
    self.showsHorizontalScrollIndicator = NO;
    self.bounces = NO;
    self.pagingEnabled = YES;
    for (UIImage *image in images) {
        NSUInteger index = [images indexOfObject:image];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
        imageView.frame = CGRectMake(index*SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGH);
        [self addSubview:imageView];
    }
}


- (void)setEnterBtn:(UIButton *)enterBtn{
    _enterBtn = enterBtn;
    [self addSubview:enterBtn];
}

- (void)setActiveIndicator:(UIImage *)activeIndicator{
    _activeIndicator = activeIndicator;
}

- (void)setUnActiveIndicator:(UIImage *)unActiveIndicator{
    _unActiveIndicator = unActiveIndicator;
}


- (void)setActiveIndicatorAtIndexPage:(NSUInteger)page{
    [[self.indicatorView subviews]makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.indicatorView removeFromSuperview];
    for (NSUInteger index=0; index<self.images.count; index++) {
        CGFloat y = 0;
        CGFloat width = index == page?self.activeIndicator.size.width:self.unActiveIndicator.size.width;
        CGFloat height = index == page?self.activeIndicator.size.height:self.unActiveIndicator.size.height;
        CGFloat x = index == 0? 0 : index*width+SPACE_BETWEEN_INDICATOR;
        UIImageView *imageView = [[UIImageView alloc]initWithImage:index == page?self.activeIndicator:self.unActiveIndicator];
        imageView.frame = CGRectMake(x, y, width, height);
        [self.indicatorView addSubview:imageView];
    }
    [self addSubview:self.indicatorView];
}

- (void)initIndicatorView{
    [self setIndicatorViewFrameWithOffset];
    self.indicatorView.frame = [self getIndicatorViewFrame];
    [self setActiveIndicatorAtIndexPage:0];
}

- (CGRect)getIndicatorViewFrame{
    NSUInteger image_count = self.images.count;
    CGFloat active_indicator_width = self.activeIndicator.size.width;
    CGFloat unactive_indicator_width = self.unActiveIndicator.size.width;
    CGFloat active_indicator_height = self.activeIndicator.size.height;
    CGFloat unactive_indicator_height = self.unActiveIndicator.size.height;
    CGFloat width = image_count <=1?0:(image_count-1)*unactive_indicator_width + active_indicator_width +(image_count-1)*SPACE_BETWEEN_INDICATOR;
    CGFloat height = MAX(active_indicator_height, unactive_indicator_height);
    CGFloat x = SCREEN_WIDTH/2 - width/2;
    CGFloat y = SCREEN_HEIGH - 100;
    return CGRectMake(x, y, width, height);
}

//根据偏移量计算Indicator位置
- (void)setIndicatorViewFrameWithOffset{
    CGFloat indicatorViewWidth = self.indicatorView.frame.size.width;
    CGFloat offsetX = self.contentOffset.x + SCREEN_WIDTH/2 - indicatorViewWidth/2;
    CGFloat y = self.indicatorView.frame.origin.y;
    CGFloat width = self.indicatorView.frame.size.width;
    CGFloat height = self.indicatorView.frame.size.height;
    self.indicatorView.frame = CGRectMake(offsetX, y, width, height);
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //当前页数
    static NSInteger previousPage = 0;
    CGFloat pageWidth = scrollView.frame.size.width;
    float fractionalPage = scrollView.contentOffset.x / pageWidth;
    NSInteger page = lround(fractionalPage);
    if (previousPage != page) {
        // Page has changed, do your thing!
        [self setActiveIndicatorAtIndexPage:page];
        // Finally, update previous page
        previousPage = page;
    }
    //设置移位
    [self setIndicatorViewFrameWithOffset];
}


@end
